﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Web.Services.Protocols;

namespace SPWebShell.cmdlet
{
    [Cmdlet(VerbsCommon.Add, "SPGroup")]
    public class AddGroup : CmdLetBase
    {
        [Parameter(Mandatory = true, HelpMessage = "The absolute URL to the SharePoint site collection. e.g. http://server/sites/SiteA/")]
        [ValidateNotNullOrEmpty]
        [Alias("siteurl")]
        public string Url { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The name of the SharePoint group which will be created")]
        [ValidateNotNullOrEmpty]
        [Alias("name", "groupname")]
        public string Group { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "Description of the SharePoint group")]
        [Alias("desc")]
        public string Description { get; set; }

        [Parameter(Mandatory = false, HelpMessage = "The login of the user who will be the owner. e.g DOMAIN\\alias. If specified then OwnerLogin should be specified")]
        [ValidateNotNullOrEmpty]
        [Alias( "user" , "login")]
        public string OwnerLogin { get; set; }

        [Parameter(Mandatory = false, HelpMessage = "The name of the group who will be the owner. If specified then OwnerLogin should be empty")]
        [ValidateNotNullOrEmpty]
        public string OwnerGroup { get; set; }
        protected override void ProcessRecord()
        {
            try
            {
                VerboseLogger("Begin cmdlet Add-SPGroup");
                VerboseLogger("Start time: " + DateTime.Now.ToString());
                ISPRepository repos = base.SPRepos;
                repos.AddGroup(this.Url, this.Group, this.OwnerLogin, this.OwnerGroup, this.Description);
                VerboseLogger("End time: " + DateTime.Now.ToString());
                base.ProcessRecord();
            }
            catch (SoapException ex)
            {
                string message = ex.Detail.OuterXml;
                throw new Exception(message, ex);
            }
            finally
            {
                VerboseLogger("Exiting cmdlet Add-SPGroup");
                WriteVerbose("End time: " + DateTime.Now.ToString());
            }
        }

    }
}
